<?php

class NCore_Row implements ArrayAccess, Iterator, Countable
{ 
	// great writeup pertaining to this type of code http://www.sitepoint.com/print/php5-standard-library
	protected $valid = FALSE;
	protected $properties = array();

	function __construct($arr = NULL)
	{
		if(!is_null($arr))
		{
			foreach($arr as $key=>$value)
			{
				if($value instanceof NCore_IValue)
				{
					$this->properties[$key] = $value->getValue();
				}
				else
				{
					$this->properties[$key] = $value;
				}
			}
		}
	}
	/** 
	* Defined by ArrayAccess interface 
	* Set a value given it's key e.g. $A['title'] = 'foo'; 
	* @param mixed key (string or integer) 
	* @param mixed value 
	* @return void 
	*/ 
	function offsetSet($key, $value)
	{ 
		if($value instanceof NCore_IValue)
		{
			$this->properties[$key] = $value->getValue();
		}
		else
		{
			$this->properties[$key] = $value;
		}
	} 
	/** 
	* Defined by ArrayAccess interface 
	* Return a value given it's key e.g. echo $A['title']; 
	* @param mixed key (string or integer) 
	* @return mixed value 
	*/ 
	function offsetGet($key)
	{ 
		return $this->properties[$key];
	} 

	/** 
	* Defined by ArrayAccess interface 
	* Unset a value by it's key e.g. unset($A['title']); 
	* @param mixed key (string or integer) 
	* @return void 
	*/ 
	function offsetUnset($key) { 
		if(array_key_exists($key, $this->properties)) 
		{ 
			unset($this->properties[$key]);
		} 
	} 
	/** 
	* Defined by ArrayAccess interface 
	* Check value exists, given it's key e.g. isset($A['title']) 
	* @param mixed key (string or integer) 
	* @return boolean 
	*/ 
	function offsetExists($offset)
	{ 
		return isset($this->properties[$offset]); 
	} 
	function current()
	{
		return current($this->properties);
	}
	function key(){
		return key($this->properties);
	}
	function next()
	{
		$this->valid = (FALSE !== next($this->properties)); 
	}
	function rewind()
	{
		$this->valid = (FALSE !== reset($this->properties)); 
	}
	function valid()
	{
		return $this->valid;
	}
	function count()
	{
		return count($this->properties);
	}
    function getKeys()
    {
        return array_keys($this->properties);
    }
    function getValues()
    {
        return array_values($this->properties);
    }
}

?>